package data.admin;

import dao.SQLiteConnection;
import gui.admin.MSWin;
import java.sql.*;

public class MS extends MSWin {

    public MS() {
        // 逾期缴款钮
        getCost().addActionListener(e->new Cost());
        //注册按钮
        getRegister().addActionListener(e -> new Register());
        //搜索按钮
        getSelect().addActionListener(e -> {
            //获取文本框的内容
            String ISBN = getT1().getText();
            String bookname = getT2().getText();
            String booktype = getT3().getText();
            String author = getT4().getText();
            //生成sql语句
            if (!(ISBN.isEmpty() && bookname.isEmpty() && booktype.isEmpty() && author.isEmpty())) {
                //非空检索
                String sql = "SELECT * FROM Book WHERE";
                if (!ISBN.isEmpty()) {
                    sql = sql + " ISBN ='" + ISBN + "'";
                }
                if (!bookname.isEmpty()) {
                    if (ISBN.isEmpty()) {
                        sql = sql + " bookname like '%" + bookname + "%'";
                    } else {
                        sql = sql + " AND bookname='" + bookname + "'";
                    }
                }
                if (!booktype.isEmpty()) {
                    if (bookname.isEmpty()) {
                        sql = sql + " booktype like '%" + booktype + "%'";
                    } else {
                        sql = sql + " AND booktype like '%" + booktype + "%'";
                    }
                }
                if (!author.isEmpty()) {
                    if (booktype.isEmpty()) {
                        sql = sql + " author like '%" + author + "%'";
                    } else {
                        sql = sql + " AND author like '%" + author + "%'";
                    }
                }
                System.out.println(sql);
                //连接数据库
                SQLiteConnection connection = new SQLiteConnection();
                connection.Connect_SQLite();
                // 读取数据库中的数据
                try {
                    Connection conn = connection.getConnection();
                    Statement stmt = conn.createStatement();
                    ResultSet rs = stmt.executeQuery(sql);
                    //清除原表格中的内容
                    getModel().setRowCount(0);
                    //添加新数据
                    ResultSetMetaData meta = rs.getMetaData();
                    int columnCount = meta.getColumnCount();
                    // 添加数据
                    while (rs.next()) {
                        Object[] rowData = new Object[columnCount];
                        for (int i = 1; i <= columnCount; i++) {
                            rowData[i - 1] = rs.getObject(i);
                        }
                        getModel().addRow(rowData);
                    }
                    getTable().setModel(getModel());    // 设置表格的数据和显示
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }

        });
        //刷新按钮
        getEmpty().addActionListener(e -> {
            getT1().setText(""); // 清除文本框内容
            getT2().setText("");
            getT3().setText("");
            getT4().setText("");
            //清除表格内容
            getModel().setRowCount(0);
            //连接数据库
            SQLiteConnection connection = new SQLiteConnection();
            connection.Connect_SQLite();
            // 读取数据库中的数据
            try {
                Connection conn = connection.getConnection();
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM Book");
                ResultSetMetaData meta = rs.getMetaData();
                int columnCount = meta.getColumnCount();
                // 添加数据
                while (rs.next()) {
                    Object[] rowData = new Object[columnCount];
                    for (int i = 1; i <= columnCount; i++) {
                        rowData[i - 1] = rs.getObject(i);
                    }
                    getModel().addRow(rowData);
                }
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            connection.closeConnection();
        });
        //借书按钮
        getBorrowbook().addActionListener(e -> {
            int row = getTable().getSelectedRow();
            //选中表格中的图书后才能打开借书界面
            if(row>=0){
                String ISBN=getTable().getValueAt(row,0).toString();
                new Borrow(ISBN);
            }
        });
        //还书按钮
        getReturnbook().addActionListener(e->new Return());
        //添加按钮
        getAddbook().addActionListener(e->new Add());
        //修改按钮
        getAltebook().addActionListener(e->{
            int row = getTable().getSelectedRow();
            //选中表格中的图书后才能打开修改界面
            if(row>=0){
                String ISBN=getTable().getValueAt(row,0).toString();
                String bookname=getTable().getValueAt(row,1).toString();
                String booktype=getTable().getValueAt(row,2).toString();
                String publisher=getTable().getValueAt(row,3).toString();
                String author=getTable().getValueAt(row,4).toString();
                String number=getTable().getValueAt(row,5).toString();
                new Alte(ISBN,bookname,booktype,publisher,author,number);
            }
        });
    }

}
